package com.handydata.antifraud.util.encrypt;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.util.encoders.Hex;

import lombok.extern.slf4j.Slf4j;

/**
 * MD5摘要工具类
 * 
 * @author licw
 *
 * @data 2017年8月30日
 *
 */
@Slf4j
public class Md5Utils {
	private Md5Utils() {
	}

	/**
	 * MD5摘要后Base64编码
	 * 
	 * @param data
	 * @return MD5摘要后Base64编码串
	 */
	public static String md5ToBas64(String data) {
		MessageDigest md = null;
		String str = null;
		try {
			md = MessageDigest.getInstance("MD5");
			str = new String(Base64.encode(md.digest(data.getBytes("UTF-8"))), "UTF-8");
		} catch (NoSuchAlgorithmException e) {
			log.error(e.getMessage());
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
		}
		return str;
	}

	/**
	 * MD5摘要后Hex编码
	 * 
	 * @param data
	 * @return MD5摘要后Hex编码串
	 */
	public static String md5ToHexStr(String data) {
		MessageDigest md = null;
		String str = null;
		try {
			md = MessageDigest.getInstance("MD5");
			str = Hex.toHexString(md.digest(data.getBytes("UTF-8")));
		} catch (NoSuchAlgorithmException e) {
			log.error(e.getMessage());
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
		}
		return str;
	}

}
